﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

using Castle.Services.Transaction;
using Castle.Facilities.IBatisNetIntegration;
using Meqs.Entity;
using Meqs.Logic.Interface;


namespace Meqs.Logic.Implement
{
    [Transactional]
    [UsesAutomaticSessionCreation]
    public abstract class AbGlobalCauseInfoLogic : CommonLogic<GlobalCauseInfo, int>, IGlobalCauseInfoLogic
    {

        #region Implementation of ICommonLogic<GlobalCauseInfo, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(GlobalCauseInfo entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(GlobalCauseInfo entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public GlobalCauseInfo FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<GlobalCauseInfo> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<GlobalCauseInfo> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        /// <summary>
        /// 查询指定的GlobalCauseInfo
        /// </summary>
        /// <param name="idList"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<GlobalCauseInfo> FindGlobalCauseInfoByIdList(string idList, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            if (idList != null && idList.Trim() != "")
            {
                ht.Add("ResearchGroupIDList", "(" + idList + ")");
            }
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<GlobalCauseInfo>("GlobalCauseInfo.FindGlobalCauseInfoByIdList", ht);
        }


        /// <summary>
        /// 查找指定学校的课程
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<GlobalCauseInfo> SelectSchoolCause(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<GlobalCauseInfo>("GlobalCauseInfo.SelectSchoolCuase", ht);
        }

        /// <summary>
        /// 查找指定学校的有效课程
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<GlobalCauseInfo> SelectSchoolCause(string schoolMark, int isEffect)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("IsEffect", isEffect);
            return FindCustomer<GlobalCauseInfo>("GlobalCauseInfo.SelectSchoolCuase", ht);
        }



        /// <summary>
        /// 获取局级考试的课程
        /// </summary>
        /// <param name="examId"></param>
        /// <returns></returns>
        public IList<GlobalCauseInfo> SelectEduExamCause(int examId)
        {
            return FindCustomer<GlobalCauseInfo>("GlobalCauseInfo.SelectEduExamCause", examId);
        }
        /// <summary>
        /// 获取校考试的课程
        /// </summary>
        /// <param name="examId"></param>
        /// <param name="schoolmark"></param>
        /// <returns></returns>
        public IList<GlobalCauseInfo> SelectSchoolExamCause(int examId, string schoolmark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("examId", examId);
            ht.Add("SchoolMark", schoolmark);
            return FindCustomer<GlobalCauseInfo>("GlobalCauseInfo.SelectSchoolExamCause", ht);
        }
        /// <summary>
        /// 插入一个新的课程信息
        /// </summary>
        /// <returns></returns>
        public int InsertNewCause(GlobalCauseInfo gcInfo, string schoolMark)
        {
            GlobalCauseInfo gc = ExecuteQueryForObject<GlobalCauseInfo>("GlobalCauseInfo.SelectMaxCauseNO", null);

            if (gc == null)
                gcInfo.CauseNo = 1;
            else
                gcInfo.CauseNo = gc.CauseNo + 1;

            StuCauseCipher sc = new StuCauseCipher();
            sc.SchoolMark = schoolMark;
            sc.Paramer = 60;
            sc.KeyWord = "BaseScore";
            sc.Explain = "学期基本分";
            int scid1 = (int)InsertCustomer("StuCauseCipher.Insert", sc);
            sc = new StuCauseCipher();
            sc.SchoolMark = schoolMark;
            sc.Paramer = 40;
            sc.KeyWord = "ExpressScore";
            sc.Explain = "平时表现分";
            int scid2 = (int)InsertCustomer("StuCauseCipher.Insert", sc);
            sc = new StuCauseCipher();
            sc.SchoolMark = schoolMark;
            sc.Paramer = 300;
            sc.KeyWord = "MaxExpressScore";
            sc.Explain = "表现分上限";
            int scid3 = (int)InsertCustomer("StuCauseCipher.Insert", sc);
            sc = new StuCauseCipher();
            sc.SchoolMark = schoolMark;
            sc.Paramer = 70;
            sc.KeyWord = "ExpressWeight";
            sc.Explain = "平时权重";
            int scid4 = (int)InsertCustomer("StuCauseCipher.Insert", sc);
            sc = new StuCauseCipher();
            sc.SchoolMark = schoolMark;
            sc.Paramer = 30;
            sc.KeyWord = "TermEndWeight";
            sc.Explain = "期末成绩权重";
            int scid5 = (int)InsertCustomer("StuCauseCipher.Insert", sc);

            int newID = Create(gcInfo);
            try
            {
                Hashtable cc = new Hashtable();
                cc.Add("SchoolMark", schoolMark);
                cc.Add("CourseID", newID);
                cc.Add("CipherID", scid1);
                InsertCustomer("CourseCipherRel.Insert", cc);
                cc = new Hashtable();
                cc.Add("SchoolMark", schoolMark);
                cc.Add("CourseID", newID);
                cc.Add("CipherID", scid2);
                InsertCustomer("CourseCipherRel.Insert", cc);
                cc = new Hashtable();
                cc.Add("SchoolMark", schoolMark);
                cc.Add("CourseID", newID);
                cc.Add("CipherID", scid3);
                InsertCustomer("CourseCipherRel.Insert", cc);
                cc = new Hashtable();
                cc.Add("SchoolMark", schoolMark);
                cc.Add("CourseID", newID);
                cc.Add("CipherID", scid4);
                InsertCustomer("CourseCipherRel.Insert", cc);
                cc = new Hashtable();
                cc.Add("SchoolMark", schoolMark);
                cc.Add("CourseID", newID);
                cc.Add("CipherID", scid5);
                InsertCustomer("CourseCipherRel.Insert", cc);
            }
            catch (Exception e)
            {
                return 0;
            }
            return newID;

        }


        /// <sammury>
        /// 更新对象的名称
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int UpdateCauseName(GlobalCauseInfo entity)
        {
            return ExecuteUpdate("GlobalCauseInfo.UpdateCauseName", entity);
        }

        /// <summary>
        /// 修改课程的有效状态
        /// </summary>
        /// <param name="isEffect"></param>
        /// <param name="RID"></param>
        /// <returns></returns>
        public int UpdateIsEffect(int isEffect, int RID)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", RID);
            ht.Add("IsEffect", isEffect);

            return ExecuteUpdate("GlobalCauseInfo.SetIsEffect", ht);
        }

        /// <summary>
        /// 交换课程编号
        /// </summary>
        /// <param name="iCauseNO"></param>
        /// <param name="iState"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int ExchangeCauseNO(GlobalCauseInfo gcInfo1, GlobalCauseInfo gcInfo2)
        {
            int iResult = Update(gcInfo1);

            if (iResult > 0)
            {
                return Update(gcInfo2);
            }
            return 0;
        }


        /// <summary>
        /// 根据课程RID以及移动状态，来上移或者下移课程编号
        /// </summary>
        /// <param name="iState"></param>
        /// <param name="iRID"></param>
        /// <returns></returns>
        public abstract int ExchangeCuaseNo(int iState, int iRID);

    }
}
